package com.litelimit.autoconfigure.configure;

import com.litelimit.core.service.loader.LuaScriptLoader;
import com.litelimit.core.strategy.core.AbstractLimitStrategy;
import com.litelimit.core.strategy.LimitStrategyManager;
import com.litelimit.core.strategy.impl.IpAbstractLimitStrategy;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

/**
 * 策略配置
 */
@Configuration
@AllArgsConstructor
public class StrategyConfiguration {

    private final StringRedisTemplate stringRedisTemplate;
    private final LuaScriptLoader luaScriptLoader;

    @Bean
    @ConditionalOnMissingBean(IpAbstractLimitStrategy.class)
    public IpAbstractLimitStrategy ipLimitStrategy(){
        return new IpAbstractLimitStrategy(stringRedisTemplate, luaScriptLoader);
    }

    @Bean
    @ConditionalOnMissingBean(LimitStrategyManager.class)
    public LimitStrategyManager limitStrategyManager(List<AbstractLimitStrategy> limitStrategies){
        return new LimitStrategyManager(limitStrategies);
    }
}
